package com.rh.controller;


import com.rh.entity.EduChapter;
import com.rh.entity.chapter.ChapterVo;
import com.rh.service.EduChapterService;
import com.rh.exception.EducationException;
import com.rh.result.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程 前端控制器
 * @author rh
 * @since 2021-09-24
 */
@Api(tags = "章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Resource
    private EduChapterService eduChapterService;
    /**
     * 章节列表
     * 要根据课程id进行查询课程里面的章节和小结
     */
    @ApiOperation(value = "1.根据课程ID获取章节列表")
    @GetMapping("getChapterVideo/{courseId}")
    public ResultVo getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list=eduChapterService.getChapterVideoByCourseId(courseId);
        return ResultVo.ok().data("allChapterVideo",list);
    }
    /**
     * 添加章节
     */
    @ApiOperation(value = "2.添加章节信息")
    @PostMapping("addChapter")
    public ResultVo addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return ResultVo.ok();
    }
    /**
     * 根据id获取章节信息
     */
    @ApiOperation(value = "3.据章节id获取章节信息")
    @GetMapping("getChapterInfo/{chapterId}")
    public ResultVo getChapterById(@PathVariable String chapterId){
        EduChapter eduChapter=eduChapterService.getById(chapterId);
        return ResultVo.ok().data("chapter",eduChapter);
    }
    /**
     * 修改章节
     */
    @ApiOperation(value = "4.修改章节信息")
    @PostMapping("updateChapter")
    public ResultVo updateChapter(@RequestBody EduChapter eduChapter){
        boolean b = eduChapterService.updateById(eduChapter);
        if(!b){
            throw new EducationException(20001,"修改章节信息失败");
        }
        return ResultVo.ok();
    }
    /**
     * 删除章节
     */
    @ApiOperation(value = "5.删除章节")
    @DeleteMapping("{chapterId}")
    public ResultVo deleteChapter(@PathVariable String chapterId){
        boolean flag=eduChapterService.deleteChapter(chapterId);
        if(flag){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
}

